#include "rkmedia_function_register.h"

extern containerInfo *above_container_func;

int rkmedia_function_achieve(int protocol, char *url_address)
{
    int ret = -1;

    ffmpegInfo *high_ffmpeg_config = new ffmpegInfo;
    assert(high_ffmpeg_config);
    memset(high_ffmpeg_config->nAddr, 0, sizeof(high_ffmpeg_config->nAddr)); // 结构体里面的数组使用之前一定要进行初始化，不然可能会有多余的内容！！！
    cout << "Get high_ffmpeg_config ..." << endl;

    high_ffmpeg_config->index = 0;
    high_ffmpeg_config->protocol = protocol;
    high_ffmpeg_config->video_codec = AV_CODEC_ID_H264;
    high_ffmpeg_config->u32Width = PIX_WIDTH_SIZE;
    high_ffmpeg_config->u32Height = PIX_HEIGHT_SIZE;
    memcpy(high_ffmpeg_config->nAddr, url_address, strlen(url_address));
    ret = init_rkmedia_ffmpeg_context(high_ffmpeg_config);
    if (ret)
    {
        cout << "init_rkmedia_ffmpeg_context high failed ..." << endl;
        return -1;
    }
    else
    {
        cout << "init_rkmedia_ffmpeg_context high success ..." << endl;
    }

    cout << "Bind module start ..." << endl;
    MPP_CHN_S stViChn;
    MPP_CHN_S stRgaChn;

    RV1126_VI_CONTAINER vi_container;
    above_container_func->get_vi_container(0, &vi_container);
    stViChn.enModId = RK_ID_VI;
    stViChn.s32ChnId = vi_container.vi_chn;

    RV1126_RGA_CONTAINER rga_container;
    above_container_func->get_rga_container(0, &rga_container);
    stRgaChn.enModId = RK_ID_RGA;
    stRgaChn.s32ChnId = rga_container.rga_chn;
    ret = RK_MPI_SYS_Bind(&stViChn, &stRgaChn);
    if (ret)
    {
        printf("Bind rga chn failed...\n");
        return -1;
    }
    else
    {
        printf("Bind rga chn success...\n");
    }

#if 0 
    MPP_CHN_S stViResChn;
    MPP_CHN_S stRgaResChn;

    RV1126_VI_CONTAINER vi_res_container;
    above_container_func->get_vi_container(1, &vi_res_container);
    stViChn.enModId = RK_ID_VI;
    stViChn.s32ChnId = vi_res_container.vi_chn;
   
    RV1126_RGA_CONTAINER rga_res_container;
    above_container_func->get_rga_container(1, &rga_res_container);
    stRgaChn.enModId = RK_ID_RGA;
    stRgaChn.s32ChnId = rga_res_container.rga_chn;
    ret = RK_MPI_SYS_Bind(&stViResChn, &stRgaResChn);
    if (ret)
    {
        printf("Bind rga_res chn failed...\n");
        return -1;
    }
    else
    {
        printf("Bind rga_res chn success...\n");
    }
#endif

    pthread_t rga_pid, rga_res_pid, venc_pid, high_push_pid;

    ret = pthread_create(&rga_pid, NULL, camera_rga_thread, NULL);
    if (ret)
    {
        printf("create camera_rga_thread failed...\n");
        return -1;
    }
    else
    {
        printf("create camera_rga_thread success...\n");
    }

#if 0
    ret = pthread_create(&rga_res_pid, NULL, camera_rga_res_thread, NULL);
    if (ret)
    {
        printf("create camera_rga_res_thread failed...\n");
        return -1;
    }
    else
    {
        printf("create camera_rga_res_thread success...\n");
    }
#endif

    ret = pthread_create(&venc_pid, NULL, camera_venc_thread, NULL);
    if (ret)
    {
        printf("create camera_venc_thread failed...\n");
        return -1;
    }
    else
    {
        printf("create camera_venc_thread success...\n");
    }

    ret = pthread_create(&high_push_pid, NULL, ffmpeg_high_video_push_thread, (void *)high_ffmpeg_config);
    if (ret)
    {
        printf("create ffmpeg_high_video_push_thread failed...\n");
        return -1;
    }
    else
    {
        printf("create ffmpeg_high_video_push_thread success...\n");
    }

    return 0;
}
